package cj.web.admin.controllers;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;

import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.ExportExcel;
import cj.web.admin.constants.KDAddressAreaEnum;
import cj.web.admin.constants.KDAddressStreetEnum;
import cj.web.admin.constants.KDBookingPaperTypeEnum;
import cj.web.admin.constants.KDBookingStatusEnum;
import cj.web.admin.constants.KDOutputTypeEnum;
import cj.web.admin.constants.KDRecovePaperWeightEnum;
import cj.web.admin.constants.MemuEnum;
import cj.web.admin.dao.KDAdviseDAO;
import cj.web.admin.dao.KDBookingCardDAO;
import cj.web.admin.dao.KDBookingPaperDAO;
import cj.web.admin.dao.KDChangeAddressDAO;
import cj.web.admin.dao.KDRecovePaperDAO;
import cj.web.admin.domain.KDBookingCard;
import cj.web.login.controllers.BaseController;


@Path("output")
public class KDOutputController extends BaseController{	
	@Autowired
	private KDBookingCardDAO kdBookingCardDAO;
	@Autowired
	private KDBookingPaperDAO kdBookingPaperDAO;
	@Autowired
	private KDChangeAddressDAO kdChangeAddressDAO;
	@Autowired
	private KDRecovePaperDAO kdRecovePaperDAO;	
	@Autowired
	private KDAdviseDAO kdAdviseDAO;
	
	@Get("show")
	public String show(Model model){
		model.add("MemuEnum",MemuEnum.kdgsdc);
        EnumSet<KDOutputTypeEnum> kdOutputTypeEnum = EnumSet.allOf(KDOutputTypeEnum.class);
        model.add("OutputEnum",kdOutputTypeEnum);
        return "portal/KDOutputList.vm";
	}
	
	 @Get("output")  
	 public void output(@Param("start")String start,@Param("end")String end, @Param("type")int type, 
			 HttpServletRequest request, HttpServletResponse response) throws Exception {  
		 String sheetName = KDOutputTypeEnum.getName(type);
		 String title = URLEncoder.encode(sheetName, "UTF-8");  // 进行转码，使其支持中文文件名  
	     response.setContentType("application/vnd.ms-excel");  // 生成提示信息 
	     response.setHeader("content-disposition", "attachment;filename=" + title + ".xls");  
	     OutputStream fOut = response.getOutputStream();
	     
	     List<Map> dataset = null;
	     ExportExcel<KDBookingCard> ex = new ExportExcel<KDBookingCard>();
	     
	     if(type == KDOutputTypeEnum.DBKQY.getValue()){
	    	 String[] headers = { "序号","报纸种类","报卡名称","订报卡卡号", "密码", "起止时间", "联系人", "联系电话", 
	        		 "城市", "区域", "街道","地址", "状态", "备注", "是否失效","创建时间", "更新时间" };
	         String[] fields = {"id","paperName","paperCardName","account","pwd","beginAt","link","phone",
	        		 "city","area","street","address","status","memo","deleteFlag","createAT","updateAT"};
	         dataset = kdBookingCardDAO.output(start,end);
	         List<Integer> ids = new ArrayList<Integer>();
	         for(Map map : dataset){
	        	 ids.add(Integer.valueOf(map.get("id").toString()));
	        	 int months = Integer.parseInt(map.get("months").toString());
	        	 String beginAt = DateUtils.formatDateStr(map.get("beginAt").toString(), DateUtils.DATE_SMALL_STR);
	        	 String tmp = DateUtils.getMinusMonth(beginAt, DateUtils.DATE_SMALL_STR, -months);
	     		 String endAt = DateUtils.getMinusTime(tmp, DateUtils.DATE_SMALL_STR, 1);
	     		 map.put("beginAt",beginAt + "~" + endAt);
	        	 map.put("city",cityWuhan);
	        	 map.put("area",KDAddressAreaEnum.getName(Integer.valueOf(map.get("area")!=null?map.get("area").toString():"-1")));
	        	 map.put("street",KDAddressStreetEnum.getName(Integer.valueOf(map.get("street")!=null?map.get("street").toString():"-1")));
	        	 map.put("status",KDBookingStatusEnum.getName(Integer.valueOf(map.get("status")!=null?map.get("status").toString():"-1")));
	        	 map.put("deleteFlag",(map.get("deleteFlag")!=null && Integer.valueOf(map.get("deleteFlag").toString())==Constants.deleteFlag.deleteFlag_1)?"有效":"失效");
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	         //批量更改状态为 已处理
	         if(ids.size() > 0){
	        	 kdBookingCardDAO.updateStatusByIds(ids);
	         }
	     }
	     else if(type == KDOutputTypeEnum.ZXDB.getValue()){
	    	 String[] headers = { "序号","报纸类型", "起止时间", "订阅期限", "订阅份数", "联系人", "移动电话","固定电话","留言","发票抬头",
	        		 "城市", "区域", "街道","地址", "价格","状态", "备注", "是否失效","创建时间", "更新时间" };
	         String[] fields = {"id","type","beginAt","subPeriod","subNum","link","phone","tel","descs","invoiceTitle",
	        		 "city","area","street","address","prize","status","memo","deleteFlag","createAT","updateAT"};
	         dataset = kdBookingPaperDAO.output(start,end);
	         for(Map map : dataset){
	        	 if(map.get("subPeriod")!=null){
	        		 float subPeriod = Float.parseFloat(map.get("subPeriod").toString());
	        		 String beginAt = DateUtils.formatDateStr(map.get("beginAt").toString(), DateUtils.DATE_SMALL_STR);
	        		 if(subPeriod == 0.5){
	        			 map.put("subPeriod","半年");
	    	        	 String tmp = DateUtils.getMinusMonth(beginAt, DateUtils.DATE_SMALL_STR, -6);
	    	     		 String endAt = DateUtils.getMinusTime(tmp, DateUtils.DATE_SMALL_STR, 1);
	    	     		 map.put("beginAt",beginAt + "~" + endAt);
	        		 }
	        		 else if( subPeriod == 1){
	        			 map.put("subPeriod","一年");
	    	        	 String tmp = DateUtils.getMinusMonth(beginAt, DateUtils.DATE_SMALL_STR, -12);
	    	     		 String endAt = DateUtils.getMinusTime(tmp, DateUtils.DATE_SMALL_STR, 1);
	    	     		 map.put("beginAt",beginAt + "~" + endAt);
	        		 }
	        	 }
	        	 map.put("type",KDBookingPaperTypeEnum.getName(Integer.valueOf(map.get("type")!=null?map.get("type").toString():"-1")));
	        	 map.put("city",cityWuhan);
	        	 map.put("area",KDAddressAreaEnum.getName(Integer.valueOf(map.get("area")!=null?map.get("area").toString():"-1")));
	        	 map.put("street",KDAddressStreetEnum.getName(Integer.valueOf(map.get("street")!=null?map.get("street").toString():"-1")));
	        	 map.put("status",KDBookingStatusEnum.getName(Integer.valueOf(map.get("status")!=null?map.get("status").toString():"-1")));
	        	 map.put("deleteFlag",(map.get("deleteFlag")!=null && Integer.valueOf(map.get("deleteFlag").toString())==Constants.deleteFlag.deleteFlag_1)?"有效":"失效");
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	     }
	     else if(type == KDOutputTypeEnum.GZTB.getValue()){
	    	 String[] headers = { "序号","报纸类型", "联系人", "移动电话","固定电话", "发票编号", "旧城市", "旧区域", "旧街道","旧地址",
	        		 "新城市", "新区域", "新街道","新地址", "状态", "备注", "是否失效","创建时间", "更新时间" };
	         String[] fields = {"id","type","link","phone","tel","invoiceNo","oldCity","oldArea","oldStreet","oldAddress",
	        		 "newCity","newArea","newStreet","newAddress","status","memo","deleteFlag","createAT","updateAT"};
	         dataset = kdChangeAddressDAO.output(start,end);
	         for(Map map : dataset){
	        	 map.put("type",KDBookingPaperTypeEnum.getName(Integer.valueOf(map.get("type")!=null?map.get("type").toString():"-1")));
	        	 map.put("oldCity",cityWuhan);
	        	 map.put("oldArea",KDAddressAreaEnum.getName(Integer.valueOf(map.get("oldArea")!=null?map.get("oldArea").toString():"-1")));
	        	 map.put("oldStreet",KDAddressStreetEnum.getName(Integer.valueOf(map.get("oldStreet")!=null?map.get("oldStreet").toString():"-1")));
	        	 map.put("newCity",cityWuhan);
	        	 map.put("newArea",KDAddressAreaEnum.getName(Integer.valueOf(map.get("newArea")!=null?map.get("newArea").toString():"-1")));
	        	 map.put("newStreet",KDAddressStreetEnum.getName(Integer.valueOf(map.get("newStreet")!=null?map.get("newStreet").toString():"-1")));
	        	 map.put("status",KDBookingStatusEnum.getName(Integer.valueOf(map.get("status")!=null?map.get("status").toString():"-1")));
	        	 map.put("deleteFlag",(map.get("deleteFlag")!=null && Integer.valueOf(map.get("deleteFlag").toString())==Constants.deleteFlag.deleteFlag_1)?"有效":"失效");
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	     }
	     else if(type == KDOutputTypeEnum.JBHS.getValue()){
	    	 String[] headers = { "序号","联系人", "联系电话", "城市", "区域", "街道","地址", "重量","留言","状态", "备注", "是否失效","创建时间", "更新时间" };
	         String[] fields = {"id","link","phone","city","area","street","address","weight","descs","status","memo","deleteFlag","createAT","updateAT"};
	         dataset = kdRecovePaperDAO.output(start,end);
	         for(Map map : dataset){
	        	 map.put("city",cityWuhan);
	        	 map.put("weight",KDRecovePaperWeightEnum.getName(Integer.valueOf(map.get("weight")!=null?map.get("weight").toString():"-1")));
	        	 map.put("area",KDAddressAreaEnum.getName(Integer.valueOf(map.get("area")!=null?map.get("area").toString():"-1")));
	        	 map.put("street",KDAddressStreetEnum.getName(Integer.valueOf(map.get("street")!=null?map.get("street").toString():"-1")));
	        	 map.put("status",KDBookingStatusEnum.getName(Integer.valueOf(map.get("status")!=null?map.get("status").toString():"-1")));
	        	 map.put("deleteFlag",(map.get("deleteFlag")!=null && Integer.valueOf(map.get("deleteFlag").toString())==Constants.deleteFlag.deleteFlag_1)?"有效":"失效");
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	     }
	     else if(type == KDOutputTypeEnum.TSJY.getValue()){
	    	 String[] headers = { "序号","联系人", "联系电话", "留言","状态", "备注", "是否失效","创建时间", "更新时间" };
	         String[] fields = {"id","link","phone","msg","status","memo","deleteFlag","createAT","updateAT"};
	         dataset = kdAdviseDAO.output(start,end);
	         for(Map map : dataset){
	        	 map.put("status",KDBookingStatusEnum.getName(Integer.valueOf(map.get("status")!=null?map.get("status").toString():"-1")));
	        	 map.put("deleteFlag",(map.get("deleteFlag")!=null && Integer.valueOf(map.get("deleteFlag").toString())==Constants.deleteFlag.deleteFlag_1)?"有效":"失效");
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	     }
         
		 fOut.flush();
		 fOut.close();
	 }  

	 
}
